package J1939;

import android.util.Log;

import kstech.com.wifitest.Point;
import kstech.com.wifitest.listener.ItemDataListener;
import kstech.com.wifitest.utils.AppContext;
import kstech.com.wifitest.utils.Globals;


public class GenericRxDBoxCallback implements IRxDBoxCallback {
	public ItemDataListener listener;

	/**
	 * ���ñ���ֵ�����ݱ������ͱ�������ĸ������͵ı���ֵ����Ϊ�������ݻ�������
	 *
	 * @param wIdx��		��������
	 * @param fValue��	����ֵ
	 */
	private void SetDataVarValue(short wIdx, float fValue) {

		J1939_DataVar_ts pDD;
		VARTYPE vt;

		pDD = J1939_Context.j1939_DataVarCfg[wIdx];
		vt = pDD.bDataType;

		if ( vt == VARTYPE.FLOAT ){
			pDD.setFloatValue(fValue);
		}
		else{
			pDD.setValue((long)fValue);
		}
		if(pDD.batteryListener != null)
		pDD.batteryListener.onBatteryChanged(fValue);
	}


	// ���캯��
	public GenericRxDBoxCallback(ItemDataListener listener) {
		// TODO Auto-generated constructor stub
		this.listener = listener;
	}

	// �ӿڷ���ʵ��
	@Override
	public Object j1939_RxDBoxCallback_tpf(Object obj) {

		long			dwValue;
		float			fValue;
		short 			i, j;

		short 			wSByte;
		byte			bBytes,  bSBit, bBits;

		if ( obj == null ) return (null);

		J1939_PGCfg_ts pPGCfg = (J1939_PGCfg_ts)obj;
		J1939_SPCfg_ts pSPCfg;

		for ( i=0; i< pPGCfg.bSPNums; i++) {

			pSPCfg = pPGCfg.pSPCfg[i];

			if ( pSPCfg == null ) continue;
			if ( pSPCfg.wRefDataIdx < 0 ) continue;

			wSByte = (short) (pSPCfg.wStartByte - 1);		// ��ʼ�ֽڣ�0����������Ϊ1����
			bSBit = (byte) (pSPCfg.bStartBit - 1);			// ��ʼλ��0����������Ϊ1����
			dwValue = 0L;

			if ( pSPCfg.bBytes == 0 ) {						// ��λȡֵ
				bBits = pSPCfg.bBits;						// λ��
				bBytes = (byte) ((bBits+bSBit+7)/8);		// �漰�ֽ���
			}
			else {
				bBytes = pSPCfg.bBytes;						// ���ֽ�ȡֵ
				bBits = (byte)(bBytes << 3 );				//
			}

			for ( j= (short)(bBytes-1); j>=0; j--) {		// �����漰���ֽڰ�С��������װ��32λ��
				dwValue =  (dwValue << 8 ) | (pPGCfg.pData[wSByte+j] & 0xFF);
			}

			dwValue >>= ( bSBit);							// ����ʼλ�������λ
			if ( bBits < 32 ) {
				dwValue &= ( ( 1 << bBits ) - 1 );			// ��ǰ������λ��0
			}

			fValue = pSPCfg.fRes * ( (float)dwValue ) + pSPCfg.fOffset;

			SetDataVarValue(pSPCfg.wRefDataIdx, fValue);

		}

		if ( pPGCfg.dwPGN >= (0xFF20 & 0x0000FFFF) &&
			 pPGCfg.dwPGN < (0xFF80 & 0x0000FFFF) ) {
			J1939_DataVar_ts lastCheckValueDSItem = AppContext.getModelFile()
					.getDataSetVO().getLastCheckValueDSItem();
			lastCheckValueDSItem.setValue(pPGCfg.dwPGN);
			J1939_Context.j1939_API.j1939_sendDatabox((short)(AppContext.getModelFile().j1939PgSetVO
					.getPg(0xFF83).wDBNumber));
		}

		if (pPGCfg.dwPGN == 0xFF81 ) {

			can_Message_ts canMsg = new can_Message_ts();
			canMsg.id_u32 = 0x18ff8235;
			canMsg.format_u8 = 1;
			canMsg.numBytes_u8 = 8;
			//canmsg.data_au8 = new byte[]{50,1,2,0,0,0,0,0};
			canMsg.data_au8 = AppContext.getModelFile().j1939PgSetVO.getPg(0xFF81).pData;
			Log.e("GenericRxDBoxCallback","ID"+Integer.toHexString(canMsg.id_u32)+"-format_u8:"+canMsg.format_u8+"-numBytes_u8:"+canMsg.numBytes_u8+"-"+canMsg.data_au8[0]+"-"+canMsg.data_au8[1]+"-"+canMsg.data_au8[2]+"-"+canMsg.data_au8[3]+"-"+canMsg.data_au8[4]+"-"+canMsg.data_au8[5]+"-"+canMsg.data_au8[6]+"-"+canMsg.data_au8[7]);
			J1939_Context.j1939_CommCfg.can_TxFIFO.add(canMsg);
		}

		if ( pPGCfg.dwPGN == 0xFF07 ) {
			long dwFrameSNO = J1939_Context.j1939_DataVarCfg[pPGCfg.pSPCfg[0].wRefDataIdx].getValue();
			long dwFrameSNO2 = J1939_Context.j1939_DataVarCfg[pPGCfg.pSPCfg[1].wRefDataIdx].getValue();
			listener.onDataChanged(new Point(dwFrameSNO,dwFrameSNO2));
		}

		if ( pPGCfg.dwPGN == 0xFF08 ) {
			long dwFrameSNO = J1939_Context.j1939_DataVarCfg[pPGCfg.pSPCfg[0].wRefDataIdx].getValue();
			long dwFrameSNO2 = J1939_Context.j1939_DataVarCfg[pPGCfg.pSPCfg[1].wRefDataIdx].getValue();
			listener.onData2Changed(new Point(dwFrameSNO,dwFrameSNO2));
		}
		if(pPGCfg.dwPGN == 0xFF09){
			Globals.progress++;
			long dwFrameSNO = J1939_Context.j1939_DataVarCfg[pPGCfg.pSPCfg[0].wRefDataIdx].getValue();
			//Log.e("0xFF09", "0xFF09------dwFrameSNO---------"+dwFrameSNO);
			//Log.e("0xFF09", "0xFF09------progress---------"+Globals.progress);
			listener.onDataNoResponse("normal",dwFrameSNO,Globals.progress);
			if ( Globals.dwExpectedFrameSNO != dwFrameSNO ) {
				for ( long l=Globals.dwExpectedFrameSNO; l<dwFrameSNO; l++) {
					listener.onDataNoResponse("err",dwFrameSNO,Globals.progress);
					//Log.e("0xFF09", "lost"+ l + "frame" );
				}
			}
			Globals.dwExpectedFrameSNO =  dwFrameSNO + 1;
		}

		return (null);
	}

}

